package com.zjitc.controller;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.zjitc.model.*;

import com.zjitc.service.UserControllerService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;
import sun.security.util.Password;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/19 15:48
 * @description:
 */
@Controller
public class UserController {
  @Autowired
  private UserControllerService userControllerService;
  private ModelAndView mv=new ModelAndView();
  @RequestMapping("/login")
  public ModelAndView login(@RequestParam("username")String username,
                            @RequestParam("password")String password,
                            HttpSession session,
                            HttpServletRequest request,
                            HttpServletResponse response) {
    User user=userControllerService.login(username,password);
    if(!user.getPassword().equals(password)||!user.getUsername().equals(username)){
      String msg="mi或者zh cuowu";
      mv.addObject("msg",msg);
      mv.setView(new InternalResourceView("/WEB-INF/user/login.jsp"));
      return mv;
    }
    session.setAttribute("user",user);
    mv.setView(new InternalResourceView("/WEB-INF/user/index.jsp"));
    return mv;
  }
  @RequestMapping("/regist")
  public ModelAndView regist(@RequestParam("username")String username,
                             @RequestParam("password")String password,
                             @RequestParam("name")    String name,
                             @RequestParam("email")   String email,
                             @RequestParam("phone")   String phone,
                             @RequestParam("addr")    String addr,
                             HttpSession session){
    User user=new User();
    user.setUsername(username);
    user.setPassword(password);
    user.setName(name);
    user.setEmail(email);
    user.setPhone(phone);
    user.setAddr(addr);
    userControllerService.regist(user);
    session.setAttribute("user",user);
    mv.setView(new InternalResourceView("/WEB-INF/user/index.jsp"));
    return mv;
  }
  @RequestMapping("findAllUser")
  public ModelAndView findAllUser(User user){
    List<User> list=userControllerService.findAllUser();
    return mv;
  }
  @RequestMapping(value = "/logout")
  public String logout(HttpSession session){
    User user=new User();
    user=null;
    session.setAttribute("user",user);
    return "/WEB-INF/user/index.jsp";
  }
  @RequestMapping("/loginUI")
  public String loginUI(){
    return "/WEB-INF/user/login.jsp";
  }
  @RequestMapping("/registUI")
  public String registUI(){
    return "/WEB-INF/user/regist.jsp";
  }
  @RequestMapping("/findCsid")
  public ModelAndView findCsid(
          @RequestParam("csid")Integer csid
  ){
    List<Product> csidproduct=userControllerService.findcsid(csid);
    System.out.println(csidproduct);
    mv.addObject("csidproduct",csidproduct);
    mv.setView(new InternalResourceView("WEB-INF/user/productList.jsp"));
    return mv;
  }
  @RequestMapping("/findProductPid")
  public ModelAndView findProductPid(
          @RequestParam("pid") Integer pid
  ){
         List<Product> ppid=userControllerService.findPid();
         mv.addObject("ppid",ppid);
         mv.setView(new InternalResourceView("WEB-INF/user/productList.jsp"));
         return mv;
  }
  @RequestMapping("/findsomeProductpid")
  public ModelAndView findsomeProductpid(){
    List<Product> ppid=userControllerService.findPid();
    mv.addObject("ppid",ppid);
    mv.setView(new InternalResourceView("WEB-INF/user/index.jsp"));
    return mv;
  }
}
